package com.codeman.crawl.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codeman.business.book.domain.Book;
import com.codeman.business.book.service.IBookService;
import com.codeman.business.crawl.service.ICrawlSingleTaskService;
import com.codeman.business.crawl.service.ICrawlSourceService;
import com.codeman.common.annotation.Log;
import com.codeman.common.core.controller.BaseController;
import com.codeman.common.core.domain.AjaxResult;
import com.codeman.common.core.page.TableDataInfo;
import com.codeman.common.enums.BusinessType;
import com.codeman.business.crawl.domain.CrawlSingleTask;
import com.codeman.business.crawl.domain.CrawlSource;


import com.codeman.crawl.service.ICrawlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @author Administrator
 */
@RestController
@RequestMapping("crawl")
public class CrawlController extends BaseController {
    @Autowired
    private ICrawlService crawlService;
    @Autowired
    private IBookService bookService;
    @Autowired
    private ICrawlSourceService crawlSourceService;
    @Autowired
    private ICrawlSingleTaskService crawlSingleTaskService;

    /**
     * 新增爬虫源
     */
    @PostMapping("addCrawlSource")
    public AjaxResult add(CrawlSource source) {
        return toAjax(crawlSourceService.save(source));
    }


    /**
     * 爬虫源分页列表查询
     */
    @GetMapping("listCrawlByPage")
    public TableDataInfo listCrawl(@RequestParam(value = "curr", defaultValue = "1") int pageNo,
                                   @RequestParam(value = "limit", defaultValue = "10") int pageSize) {
        Page<CrawlSource> page = new Page<>(pageNo, pageSize);
        IPage<CrawlSource> list = crawlSourceService.page(page, null);
        return getDataTable(list.getRecords());
    }

    /**
     * 开启或停止爬虫
     */
    @PostMapping("openOrCloseCrawl")
    public AjaxResult openOrCloseCrawl(Integer sourceId, String sourceStatus) {

        crawlService.openOrCloseCrawl(sourceId, sourceStatus);

        return AjaxResult.success();
    }

    @PostMapping("addCrawlSingleTask")
    public AjaxResult add(CrawlSingleTask singleTask) {
        return toAjax(crawlSingleTaskService.save(singleTask));
    }


    /**
     * 查询单本采集任务列表
     */
    @GetMapping("listCrawlSingleTaskByPage")
    public TableDataInfo listCrawlSingleTask(@RequestParam(value = "curr", defaultValue = "1") int pageNo,
                                             @RequestParam(value = "limit", defaultValue = "10") int pageSize) {
        Page<CrawlSingleTask> page = new Page<>(pageNo, pageSize);
        IPage<CrawlSingleTask> list = crawlSingleTaskService.page(page, null);
        return getDataTable(list.getRecords());
    }

    /**
     * 删除采集任务
     */
    @DeleteMapping("delCrawlSingleTask/{id}")
    public AjaxResult delCrawlSingleTask(@PathVariable("id") Long id) {
        return toAjax(crawlSingleTaskService.removeById(id));
    }
}
